<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Models\OpenidUnionid;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * unionID与openid绑定，确认是某个用户
     * @param Request $request
     * @return array
     */
    public function bind(Request $request)
    {
        $openid = $request->get('openid');
        $unionid = $request->get('unionid');
        $ou = OpenidUnionid::where('openid', $openid)->where('unionid', $unionid)->first();
        if (empty($ou)) {
            $ou = new OpenidUnionid();
            $ou['openid'] = $openid;
            $ou['unionid'] = $unionid;
            $ou->save();
        }
        $u = User::where('unionid', $unionid)->first();
        if (empty($u)) {
            return rsp(40401);
        }
        if (empty($u['token'])) {
            $u['token'] = md5(time());
            $u->save();
        }
        return rsp($u);
    }
}
